package com.njust.RateMyTray_backend.controller;

import com.njust.RateMyTray_backend.common.Result;
import com.njust.RateMyTray_backend.config.CurrentUser;
import com.njust.RateMyTray_backend.dto.FavoriteCreateDTO;
import com.njust.RateMyTray_backend.entity.Favorite;
import com.njust.RateMyTray_backend.entity.User;
import com.njust.RateMyTray_backend.service.FavoriteService;
import com.njust.RateMyTray_backend.vo.FavoriteVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/favorites")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    @PostMapping
    public ResponseEntity<Result<Map<String, Long>>> addFavorite(@CurrentUser User currentUser, @RequestBody FavoriteCreateDTO createDTO) {
        Favorite newFavorite = favoriteService.addFavorite(currentUser.getId(), createDTO);
        
        Map<String, Long> responseData = Collections.singletonMap("favoriteId", newFavorite.getId());
        Result<Map<String, Long>> result = Result.success(201, "收藏成功", responseData);
        
        return new ResponseEntity<>(result, HttpStatus.CREATED);
    }

    @DeleteMapping("/{favoriteId}")
    public ResponseEntity<Void> removeFavorite(@CurrentUser User currentUser, @PathVariable Long favoriteId) {
        favoriteService.removeFavorite(currentUser.getId(), favoriteId);
        return ResponseEntity.noContent().build();
    }

    @GetMapping
    public Result<List<FavoriteVO>> getFavorites(@CurrentUser User currentUser, @RequestParam(required = false) String targetType) {
        List<FavoriteVO> favorites = favoriteService.getFavorites(currentUser.getId(), targetType);
        return Result.success(favorites);
    }
} 